package cn.fsd.controller;

import cn.fsd.domain.Suppliers;
import cn.fsd.query.delivery.SupplierQuery;
import cn.fsd.service.impl.SupplierServiceImpl;
import cn.fsd.vo.ResultBean;
import io.swagger.annotations.ApiOperation;
import java.util.Objects;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/supplier")
public class SupplierController {

  @Autowired
  SupplierServiceImpl supplierService;

  @GetMapping("/all")
  @ApiOperation(value = "查询全部供应商")
  public ResultBean getAllSuppliers() {
    return ResultBean.success(supplierService.getAllSuppliers());
  }

  @GetMapping("/{id}")
  @ApiOperation(value = "根据ID查询供应商")
  public ResultBean getSupplierById(@PathVariable("id")String id){
    return ResultBean.success(supplierService.getSupplierByID(id));
  }

  @PostMapping("/page")
  @ApiOperation(value = "分页查询供应商")
  public ResultBean getSuppliersByPage(@RequestBody SupplierQuery query) {
    return ResultBean.success(supplierService.getSuppliersByPage(query));
  }

  @PostMapping
  @ApiOperation(value = "新增供应商")
  public ResultBean addNewSupplier(@RequestBody Suppliers supplier) {
    if (StringUtils.isEmpty(supplier.getSupplier_name())) {
      return ResultBean.error("供应商名称不能为空");
    } else if (StringUtils.isEmpty(supplier.getSupplier_address())) {
      return ResultBean.error("供应商地址不能为空");
    } else if (StringUtils.isEmpty(supplier.getBank_card_num())) {
      return ResultBean.error("供应商银行卡号不能为空");
    } else if(StringUtils.isEmpty(supplier.getSupplier_contact())){
      return ResultBean.error("供应商联系人不能为空");
    } else if(StringUtils.isEmpty(supplier.getSupplier_phone())){
      return ResultBean.error("供应商联系电话不能为空");
    }
    Suppliers s = supplierService.getSupplierByName(supplier.getSupplier_name());
    if (s != null) {
      return ResultBean.error("供应商名称重复");
    }

    return ResultBean.success((Object)supplierService.addNewSupplier(supplier));
  }

  @DeleteMapping("/{id}")
  @ApiOperation(value = "根据id删除供应商")
  public ResultBean deleteSupplier(@PathVariable("id") String supplierId) {
    supplierService.removeSupplierById(supplierId);
    return ResultBean.success();
  }

  @PutMapping
  @ApiOperation(value = "更新供应商信息")
  public ResultBean updateSupplier(@RequestBody Suppliers supplier) {
    if (StringUtils.isEmpty(supplier.getSupplier_name())) {
      return ResultBean.error("供应商名称不能为空");
    } else if (StringUtils.isEmpty(supplier.getSupplier_address())) {
      return ResultBean.error("供应商地址不能为空");
    } else if (StringUtils.isEmpty(supplier.getBank_card_num())) {
      return ResultBean.error("供应商银行卡号不能为空");
    } else if(StringUtils.isEmpty(supplier.getSupplier_phone())){
      return ResultBean.error("供应商手机号不能为空");
    }
    Suppliers s = supplierService.getSupplierByName(supplier.getSupplier_name());
    if (s != null && !Objects.equals(s.getSupplier_id(), supplier.getSupplier_id())) {
      return ResultBean.error("供应商名称重复");
    }
    supplierService.updateSupplier(supplier);
    return ResultBean.success();
  }
}
